<style type="text/css">
    .action-cell {
        margin-bottom: 20px;
    }

    .search-block {
        display: flex;
        margin-bottom: 10px;
    }

    .input-search {
        margin-right: 10px;
    }
</style>
<template>
    <div style="min-width: 1024px">
        <h4>战备物资管理</h4>
        <Alert show-icon>
            此处用于编辑战备类物资的管理。
        </Alert>
        <Tabs @on-click="tabCLickEvent" type="card">
            <TabPane v-for="(tab,index) in tabPaneArr" :key="(index+1)" :label="tab" :name="tab">
                <ButtonGroup>
                    <Button type="success" @click="addModal=true">
                        <Icon type="md-add-circle"/>
                        添加
                    </Button>
                    <Button type="warning" :disabled="deleteBtnEnable" @click="remove">
                        <Icon type="md-close-circle"/>
                        删除
                    </Button>
                </ButtonGroup>
                <Row>
                    <Col >
                        <Table :context="self" :data="tableData" :columns="tableColumns" stripe border
                               @on-select="tableItemChecked" @on-select-cancel="tableItemChecked"
                               @on-select-all="tableItemChecked"
                               @on-select-all-cancel="tableItemChecked"
                               style="margin-top:15px;">
                        </Table>
                    </Col>
                </Row>
            </TabPane>
        </Tabs>
        <!--添加-->
        <Modal v-model="addModal" title="添加" @on-ok="handleSubmitAdd('formValidate')"
               @on-cancel="addModal =false">
            <Form ref="formValidate" :model="formValidate" :label-width="100">
                <Form-item label="物资名称" prop="Title">
                    <i-input v-model="formValidate.Title" placeholder="请输入物资名称"></i-input>
                </Form-item>
                <Form-item label="联系信息" prop="Productcontact">
                    <i-input v-model="formValidate.Productcontact"  placeholder="请输入联系人或其它标识信息"></i-input>
                </Form-item>
                <Form-item label="描述" prop="Infomation">
                    <i-input v-model="formValidate.Infomation" placeholder="请输入描述"></i-input>
                </Form-item>
                <Form-item label="物资数量" prop="ItemCount">
                    <i-input v-model="formValidate.ItemCount" type="number"  placeholder="请输入物资数量"></i-input>
                </Form-item>
                <Form-item label="数量计量单位" prop="Itemunit">
                    <i-input v-model="formValidate.Itemunit"  placeholder="请输入数量计量单位(个、套、台等 )"></i-input>
                </Form-item>
                <Form-item label="厂家或其标识信息" prop="Product">
                    <i-input v-model="formValidate.Product"  placeholder="请输入厂家或其标识信息"></i-input>
                </Form-item>
            </Form>
        </Modal>
        <!--编辑-->
        <Modal v-model="editModal" title="编辑" @on-ok="handleSubmitEdit('formValidateEdit')"
               @on-cancel="editModal =false">
            <Form ref="formValidateEdit" :model="formValidateEdit" :label-width="100">
                <Form-item label="物资名称" prop="Title">
                    <i-input v-model="formValidateEdit.Title" placeholder="请输入物资名称"></i-input>
                </Form-item>
                <Form-item label="联系信息" prop="Productcontact">
                    <i-input v-model="formValidateEdit.Productcontact"  placeholder="请输入联系人或其它标识信息"></i-input>
                </Form-item>
                <Form-item label="描述" prop="Infomation">
                    <i-input v-model="formValidateEdit.Infomation" placeholder="请输入描述"></i-input>
                </Form-item>
                <Form-item label="物资数量" prop="ItemCount">
                    <i-input v-model="formValidateEdit.ItemCount" type="number"  placeholder="请输入物资数量"></i-input>
                </Form-item>
                <Form-item label="数量计量单位" prop="Itemunit">
                    <i-input v-model="formValidateEdit.Itemunit"  placeholder="请输入数量计量单位(个、套、台等 )"></i-input>
                </Form-item>
                <Form-item label="厂家或其标识信息" prop="Product">
                    <i-input v-model="formValidateEdit.Product"  placeholder="请输入厂家或其标识信息"></i-input>
                </Form-item>
            </Form>
        </Modal>
    </div>
</template>
<script>
    import apiUtil from '../../assets/utils/api_util';

    export default {
        data() {
            return {
                //物资分类id
                sortTypeId: 5,
                self: this,
                tableData: [],
                tabPaneArr:[
                    '枪械','战备金','药品'
                ],
                //添加
                formValidate: {
                    //添加字段
                    Title: '',//物资名称
                    Infomation: '',//描述
                    //（可选，未来扩展用，JSON段） OtherInfo: ''
                    ItemCount: '',//物资数量
                    Itemunit: '',//数量计量单位，如“个、套、台等 ”
                    Product: '',//厂家或其标识信息
                    Productcontact: '',//联系人或其它标识信息
                },
                chooseAreaArr: [],
                addModal: false,
                //编辑
                formValidateEdit: {
                    Title: '',//物资名称
                    Infomation: '',//描述
                    //（可选，未来扩展用，JSON段） OtherInfo: ''
                    ItemCount: '',//物资数量
                    Itemunit: '',//数量计量单位，如“个、套、台等 ”
                    Product: '',//厂家或其标识信息
                    Productcontact: '',//联系人或其它标识信息
                },
                editModal: false,
                //操作按钮状态管理
                deleteBtnEnable: true,
                selectedItems: [],
                //表格
                total: 0,
                current: 1,
                tableColumns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '物资分类名称',
                        key: 'Title',
                        align: 'center',
                    },
                    {
                        title: '厂家或其标识信息',
                        key: 'Product',
                        align: 'center',
                    },
                    {
                        title: '联系人或其它标识信息',
                        key: 'Productcontact',
                        align: 'center',
                    },
                    {
                        title: '物资数量',
                        key: 'Itemcount',
                        align: 'center',
                    },
                    {
                        title: '数量计量单位',
                        key: 'Itemunit',
                        align: 'center',
                    },
                    {
                        title: '操作',
                        key: 'action',
                        width: 220,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                    h('Button', {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            'margin-right': '10px',
                                        },
                                        on: {
                                            click: () => {
                                                this.editClick(params.row)
                                            }
                                        }
                                    }, '编辑'),
                                ]
                            )
                        }
                    },
                ]
            }
        },
        mounted() {
            this.getGetSourceTypeId('枪械');
        },
        methods: {
            //跳转
            jumpPage: function (id, url) {
                //跳转
                let route = '';
                if (id > 0) {
                    route = {
                        path: url,
                        query: {
                            id: id
                        },
                    };
                } else {
                    route = {
                        path: url,
                    };
                }

                this.$router.push(route);
            },
            //获取物资对应id
            getGetSourceTypeId:function(typeName){
                let that = this;

                let para = {
                    typeName: typeName
                };

                let params = that.$qs.stringify(para);

                this.$Loading.start();

                apiUtil.get(that, apiUtil.urls.GetSourceTypeId, para, function (res) {
                    that.sortTypeId = res.Msg;
                    that.mockTableData();
                });
            },
            //tab点击
            tabCLickEvent: function (name) {
                this.getGetSourceTypeId(name);
            },
            mockTableData() {
                let that = this;

                let para = {
                    sortTypeId: this.sortTypeId
                };
                let params = that.$qs.stringify(para);

                this.$Loading.start();

                this.tableData = [];
                apiUtil.get(that, apiUtil.urls.MaterialList, para, function (res) {
                    that.$Loading.finish();
                    that.tableData = res.Msg.Arrays;
                });

            },
            changePage(row) {
                // 这里直接更改了模拟的数据，真实使用场景应该从服务端获取数据
                console.log("row", row);
                this.current = row;
                this.mockTableData();
            },
            // 表格内选中及取消选中操作
            tableItemChecked: function (selection, row) {

                this.selectedItems = selection;
                console.log('选择删除项:', selection);

                if (this.selectedItems.length > 1) {
                    // 此处添加不要多选的提示
                    this.$Message.warning('此处不允许批量删除 ')
                    this.deleteBtnEnable = true;

                } else {
                    this.deleteBtnEnable = false;
                }
            },
            //删除
            remove: function () {
                let self = this;
                let that = this;
                this.$Modal.confirm({
                    title: '操作警告',
                    content: '即将删除选中的区域项，该操作不可逆转，请确认您的操作。',
                    onOk: function () {
                        this.$Loading.start();
                        var selectedItemIds = "";
                        for (var i = 0; i < that.selectedItems.length; i++) {
                            selectedItemIds += that.selectedItems[i].Id.toString() + ",";
                        }
                        selectedItemIds = selectedItemIds.substring(0, selectedItemIds.length - 1);
                        let para = {id: selectedItemIds};
                        let params = that.$qs.stringify(para);

                        apiUtil.post(that, apiUtil.urls.DelMaterialItem, params, function (res) {
                            that.$Message.success('删除成功。');
                            that.mockTableData();
                        });
                    }
                });
            },
            //添加
            handleSubmitAdd(name) {
                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.addData(model);
                });
            },
            addData: function (model) {
                let that = this;

                model.Sourcetypeid =this.sortTypeId;
                let params = that.$qs.stringify(model);
                this.$Loading.start();

                console.log(params);

                apiUtil.post(that, apiUtil.urls.AddMaterialItem, params, function (res) {
                    that.$Loading.finish();
                    that.addModal = false;
                    that.mockTableData();
                    that.$Message.info('添加成功 ');
                    that.handleReset('formValidate');

                });
            },
            //重置表单
            handleReset(name) {
                this.$refs[name].resetFields();
            },
            //点击编辑
            editClick: function (row) {

                this.editModal = true;
                this.formValidateEdit.Title = row.Title;
                this.formValidateEdit.Infomation = row.Infomation;
                this.formValidateEdit.ItemCount = row.Itemcount;
                this.formValidateEdit.Itemunit = row.Itemunit;
                this.formValidateEdit.Product = row.Product;
                this.formValidateEdit.Productcontact = row.Productcontact;
                this.formValidateEdit.Sourcetypeid = row.Sourcetypeid;
                this.formValidateEdit.Id = row.Id;

            },
            handleSubmitEdit(name) {
                let that = this;
                let model = this.$refs[name].model;
                this.$refs[name].validate((valid) => {
                    that.comfirmModifyPSEdit(model);
                })
            },
            comfirmModifyPSEdit: function (model) {
                let that = this;
                let params = {};

                params = that.$qs.stringify(model);
                console.log('params:', params)
                apiUtil.post(that, apiUtil.urls.EditMaterialItem, params, function (res) {
                    that.$Message.info('修改成功 ')
                    that.mockTableData();

                });
            },

        },
    }
</script>
